
class Money implements Cloneable{
    public double money = 19.9;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Person implements Cloneable{
    public String name;
    public Money m;
    int age;
    Person(String name, int age){
        this.name = name;
        this.age = age;
        this.m = new Money();
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        //不同包中的子类需要使用super调用
        Person tmp = (Person) super.clone();
        tmp.m = (Money) this.m.clone();
        return tmp;
    }
}
public class Main {

    //深拷贝与浅拷贝
    //浅拷贝,只是对person对象进行克隆一份，并没有对Money引用类型对象克隆一份
    //深拷贝 ...
    public static void main(String[] args) throws CloneNotSupportedException{
        Person person1 = new Person("zs",10);
        Person person2 = (Person)person1.clone();
        System.out.println(person1.m.money);
        System.out.println(person2.m.money);
        person2.m.money = 99.99;
        System.out.println("===============");
        System.out.println(person1.m.money);
        System.out.println(person2.m.money);
    }


    //clone
    public static void main1(String[] args) throws CloneNotSupportedException {
//        Person person = new Person("zhangsan", 20);
//        System.out.println(person);

        Person person1 = new Person("lisi",10);
        Person person2 = (Person)person1.clone();
        System.out.println(person2);
        System.out.println(person1);
        System.out.println(person1.hashCode());
        System.out.println(person2.hashCode());
    }
}